import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:largefriends/config/lf_route.dart';
import 'package:largefriends/entity/entities/lf_user_entity.dart';
import 'package:largefriends/net/error_handle.dart';
import 'package:largefriends/net/lf_api_url.dart';
import 'package:largefriends/net/lf_dio_util.dart';
import 'package:largefriends/net/lf_image_upload.dart';
import 'package:largefriends/pages/lf_register/pages/lf_register_account.dart';
import 'package:largefriends/pages/lf_register/pages/lf_register_photo.dart';
import 'package:largefriends/service/lf_auth_service.dart';
import 'package:largefriends/utils/lf_dialog_utils.dart';
import 'package:largefriends/utils/lf_keyboard.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

toRegisterPage() {
  Get.toNamed(LFRoute.pathRegister);
}

class LfRegisterLogic extends GetxController {
  PageController pageController = PageController();
  RxInt currentPage = 0.obs;

  RxString email = "".obs;
  FocusNode emailFocusNode = FocusNode();
  TextEditingController emailController = TextEditingController();
  RxString checkedEmail = "".obs;

  RxString password = "".obs;
  FocusNode passwordFocusNode = FocusNode();
  TextEditingController passwordController = TextEditingController();

  RxString username = "".obs;
  FocusNode usernameFocusNode = FocusNode();
  TextEditingController usernameController = TextEditingController();
  RxString checkedUsername = "".obs;

  RxInt gender = 0.obs;
  FocusNode genderFocusNode = FocusNode();
  TextEditingController genderController = TextEditingController();

  Rxn<DateTime?> birthday = Rxn<DateTime?>(null);
  FocusNode birthdayFocusNode = FocusNode();
  TextEditingController birthdayController = TextEditingController();

  RxList<AssetEntity> photos = RxList.empty(growable: true);
  Rxn<AssetEntity> image = Rxn();

  final List<Widget> pages = [
    const LfRegisterAccount(),
    // const LfRegisterInfo(),
    const LfRegisterPhotoPage()
  ];

  register() async {
    if (isShowSoftKeyboard()) {
      closeSoftKeyboard();
    }

    await uploadFile(fileList: [image.value!]).then((value) async {
      if (value.isEmpty) {
        hideLoading();
        return;
      }
      Map<String, dynamic> params = {
        "user[matchGender]": gender.value == 1 ? 2 : 1,
        "user[gender]": gender.value,
        "user[avatar]": value.first.attachId,
        "user[username]": username.value.trim(),
        "user[password]": password.value.trim(),
        "user[email]": email.value.trim(),
      };
      await LfDio().post<LfUserEntity>(url: apiRegister, params: params).then(
          (value) {
        hideLoading();
        LfAuthService service = Get.find();
        service.setUserEntity(value);
        service.updateLastLoginInfo(email.value);
        Get.offAllNamed(LFRoute.pathHome);
      }, onError: (error) {
        hideLoading();
        if (error is NetError) {
          showSnackBar(error.msg);
        }
      });
    }, onError: (error) {
      hideLoading();
      if (error is NetError) {
        showSnackBar(error.msg);
      }
    });
  }

  next() {
    closeSoftKeyboard();
    pageController.nextPage(
      duration: const Duration(milliseconds: 500),
      curve: Curves.decelerate,
    );
  }

  previous() {
    closeSoftKeyboard();
    if (currentPage.value == 0) {
      Get.back();
    } else {
      pageController.previousPage(
        duration: const Duration(milliseconds: 500),
        curve: Curves.decelerate,
      );
    }
  }

  @override
  void onClose() {
    emailController.dispose();
    passwordController.dispose();
    usernameFocusNode.dispose();
    pageController.dispose();
    emailFocusNode.dispose();
    usernameController.dispose();
    passwordFocusNode.dispose();
    genderFocusNode.dispose();
    genderController.dispose();
    super.onClose();
  }
}
